package alg20241126;

public class Text3 {
    public String convert(String ss, int numRows) {
        char[] s = ss.toCharArray();
        int n = ss.length();
        //
        StringBuilder str = new StringBuilder();
        if(numRows == 1) return ss;
        //算出公差
        int d = 2 * numRows - 2;
        //先填第一行
        for (int i = 0; i < n; i += d) {
            str.append(s[i]);
        }
        //填k行
        for (int k = 1; k < numRows-1; k++) {
            for (int i = k, j = d - k; i < n || j < n; i += d, j += d) {
                if(i < n) str.append(s[i]);
                if(j < n) str.append(s[j]);
            }
        }
        //填最后一行
        for (int i = numRows - 1; i < n; i += d) {
            str.append(s[i]);
        }
        return str.toString();
    }
}
